﻿package net.nicoptere.media.video.tracker3d 
{
	import flash.display.Bitmap;
	import org.papervision3d.core.animation.clip.AnimationClip3D;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.MovieMaterial;
	
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.DAE;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.cameras.CameraType;
	
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import net.nicoptere.media.video.tracker3d.Tracker3D;
	
	import flash.geom.Rectangle;
	/**
	 * @author Nicolas Barradeau
	 */
	public class DAELoader extends BasicView
	{
		
		public var _model:DAE;
		public function get model():DAE { return _model; };
		private var _totalFrames:int;
		public function get totalFrames():int { return _totalFrames; };
		
		protected var group:DisplayObject3D;
		
		public function DAELoader() 
		{
			//super(viewportWidth, viewportHeight, scaleToStage, interactive, cameraType);
			super(Tracker3D.viewportWidth, Tracker3D.viewportHeight, true, false, CameraType.FREE );
			camera.fov = Tracker3D.FOV;
			
			//au cas ou on doive flipper la scène ( model.yUp == false )
			group = new DisplayObject3D( 'group' );
			scene.addChild( group );
			
			//le DAE 
			_model= new DAE( false, 'model', false );
			group.addChild( _model );
			
		}
		public function load(url:String) : void
		{
			
			_model.removeEventListener( FileLoadEvent.LOAD_COMPLETE, onLoadComplete );
			_model.addEventListener( FileLoadEvent.LOAD_COMPLETE, onLoadComplete );
			
			_model.removeEventListener( FileLoadEvent.LOAD_PROGRESS, onLoadProgress );
			_model.addEventListener( FileLoadEvent.LOAD_PROGRESS, onLoadProgress );
			
			_model.load( url );
			
		}
		
		private function onLoadProgress( e:FileLoadEvent ):void
		{
			
			dispatchEvent( e );
			
		}
		private function onLoadComplete( e:FileLoadEvent ):void
		{
			
			_model.removeEventListener( FileLoadEvent.LOAD_PROGRESS, onLoadProgress );
			_model.removeEventListener( FileLoadEvent.LOAD_COMPLETE, onLoadComplete );
			
			//////////////////////////////////
			
			if ( Tracker3D.FIX_CAMERA )
			{
				
				camera.x = camera.y = camera.z = 0;
				camera.rotationX = camera.rotationY = camera.rotationZ = 0;
				
			}
			
			//////////////////////////////////
			
			if ( _model.yUp == false )
			{
				
				group.rotationX = 180;
				group.rotationY = 0;
				group.rotationZ = 180;
				
			}
			
			//////////////////////////////////
			
			
			
			var fps:int = Tracker3D.FPS;
			
			var end:Number =  _model.animation.endTime;
			var frameTime:Number = ( 1 / fps );
			
			_totalFrames = int( end / frameTime ) + 1;
			
			var i:int;
			var currentTime:Number = 0;
			for ( i = 0; i < totalFrames; i++ )
			{
				
				_model.animation.addClip( new AnimationClip3D( String( i ), currentTime, ( currentTime + frameTime ) ) );
				currentTime += frameTime;
				
			}
			if ( Tracker3D.debug ) trace( 'the animation contains ',  _model.animation.clips.length, ' frames' );
			
			//////////////////////////////////
			
			dispatchEvent( new Event( Event.COMPLETE ) );
			
		}
		
		
	}

}